package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.article.dto.ArticleDto;
import com.heima.article.dto.ArticleHomeDto;
import com.heima.article.dto.ArticleInfoDto;
import com.heima.article.entity.ApArticle;
import com.heima.article.entity.ApArticleContent;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.IApArticleContentService;
import com.heima.article.service.IApArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author gxm
 * @since 2023-12-18
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements IApArticleService {

    @Autowired
    private IApArticleContentService apArticleContentService;

    @Autowired
    private CreateHtmlTask createHtmlTask;

    @Override
    public ResponseResult<Long> saveArticle(ArticleDto dto) {
        if (dto.getId() == null) {// 新增
            ApArticle apArticle = new ApArticle();
            BeanUtils.copyProperties(dto, apArticle);
            apArticle.setCreatedTime(new Date());
            this.save(apArticle);

            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());

            apArticleContentService.save(apArticleContent);
            createHtmlTask.createHtml(apArticle,apArticleContent.getContent());
            return ResponseResult.okResult(apArticle.getId());

        } else {  //修改
            ApArticle apArticle = new ApArticle();
            BeanUtils.copyProperties(dto, apArticle);
            apArticle.setCreatedTime(new Date());
            this.updateById(apArticle);

            LambdaQueryWrapper<ApArticleContent> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ApArticleContent::getArticleId, dto.getId());
            ApArticleContent apArticleContent = apArticleContentService.getOne(wrapper);
            if (apArticleContent != null) {
                apArticleContent.setContent(dto.getContent());
                apArticleContentService.updateById(apArticleContent);
            }
            return ResponseResult.okResult(apArticle.getId());
        }
    }

    //加载文章 1: 首次加载  2 加载更多   3 加载更新
    @Override
    public ResponseResult loadArticle(ArticleHomeDto dto, Integer type) {
        //1. 设置分页条件
        IPage<ApArticle> page = new Page<>(1, dto.getSize());

        //2. 设置业务条件
        LambdaQueryWrapper<ApArticle> wrapper = new LambdaQueryWrapper<>();
        //如果dto中的channelId不为0时 根据频道查询
        wrapper.eq(dto.getChannelId() != 0, ApArticle::getChannelId, dto.getChannelId());
        //第一次加载和加载更新  发布时间 > maxTime
        wrapper.gt(type != 2, ApArticle::getPublishTime, dto.getMaxTime());
        //加载更多 发布时间< minTime
        wrapper.lt(type == 2, ApArticle::getPublishTime, dto.getMinTime());
        //两个隐藏的条件 1、是否删除 2、是否下架
        wrapper.eq(ApArticle::getIsDelete, false);
        wrapper.eq(ApArticle::getIsDown, false);
        //根据发布时间降序查询
        wrapper.orderByDesc(ApArticle::getPublishTime);

        //3. 执行查询
        this.page(page, wrapper);

        //4. 返回结果
        return ResponseResult.okResult(page.getRecords());
    }

    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {
        // 根据id查询文章
        ApArticle apArticle = this.getById(dto.getArticleId());
        if (apArticle == null || apArticle.getIsDelete() || apArticle.getIsDown()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        // 根据id查询文章内容
        LambdaQueryWrapper<ApArticleContent> query = new LambdaQueryWrapper<>();
        query.eq(ApArticleContent::getArticleId, dto.getArticleId());
        ApArticleContent articleContent = apArticleContentService.getOne(query);

        // 按照前端的格式拼接map
        Map<String, Object> map = new HashMap<>();
        map.put("config", apArticle);
        map.put("content", articleContent);
        return ResponseResult.okResult(map);
    }
}
